package com.baimifan.renovation.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baimifan.renovation.R;
import com.baimifan.renovation.model.AddressData;
import com.baimifan.renovation.view.AddressHolder;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/06.
 */

public class AddressAdapter extends BaseAdapter {
    private ArrayList<AddressData> mlists;
    private Float w, h;

    public AddressAdapter (ArrayList<AddressData> list, Float w, Float h) {
        this.mlists = list;
        this.w = w;
        this.h = h;
    }

    @Override
    public int getCount() {
        return mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return mlists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddressHolder holder;
        if (convertView == null) {
            holder = new AddressHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_item, parent, false);
            holder.address = (TextView) convertView.findViewById(R.id.address_item_name);
            convertView.setTag(holder);
        } else {
            holder = (AddressHolder) convertView.getTag();
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (position == 0) {
            params.setMargins((int)(w*15), (int)(h*16), 0, (int)(h*22));
        } else {
            params.setMargins((int)(w*15), 0, 0, (int)(h*22));
        }
        holder.address.setLayoutParams(params);
        holder.address.setText(mlists.get(position).getName());
        return convertView;
    }
}
